package dao

import (
	"go-weixin/app/music/model"
	"go-weixin/common/chendb"
)

type MusicDao struct {
	engine chendb.ChenDb
}

func NewMusicDao(engine chendb.ChenDb) *MusicDao {
	return &MusicDao{
		engine: engine,
	}
}

func (d MusicDao) MusicAdd(music *model.MusicModel) (int64, error) {
	return d.engine.Insert(music)
}

func (d MusicDao) MusicExistByTitle(title string) (bool, error) {
	return d.engine.Exist(&model.MusicModel{
		Title: title,
	})
}

func (d MusicDao) MusicGetByTitle(title string, music *model.MusicModel) (bool, error) {
	return d.engine.Where("title = ?", title).Get(music)
}

func (d MusicDao) MusicGetByMd5(md5 string, music *model.MusicModel) (bool, error) {
	return d.engine.Where("md5 = ?", md5).Get(music)
}

func (d MusicDao) MusicList(limit int64, offset int64, musicList *[]model.MusicModel) error {
	return d.engine.Limit(int(limit), int(offset)).Find(musicList)
}

func (d MusicDao) MusicDeleteByMd5(md5 string) (int64, error) {
	return d.engine.Delete(&model.MusicModel{
		Md5: md5,
	})
}

func (d MusicDao) MusicLikeByTitle(title string, limit int, offset int, musicList *[]model.MusicModel) error {
	return d.engine.Where("title LIKE ?", "%"+title+"%").Limit(limit, offset).Find(musicList)
}

func (d MusicDao) MusicLikeByArtist(artist string, limit int, offset int, musicList *[]model.MusicModel) error {
	return d.engine.Where("artist LIKE ?", "%"+artist+"%").Limit(limit, offset).Find(musicList)
}
